import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';

FormData getSign(Map parameter) {
  var key = '2020dmshop2020';
  var timestamp = new DateTime.now().millisecondsSinceEpoch;
  var versionNumber = 'V1';
  var dataType = 'JSON';
  parameter['data_type'] = dataType;
  parameter['version'] = versionNumber;
  parameter['timestamp'] = "$timestamp";
  List<String> allKeys = [];
  parameter.forEach((key, value) {
    if (!(value is MultipartFile)) {
      allKeys.add(key.toString() + value.toString());
    }
  });
  allKeys.sort((obj1, obj2) {
    return obj1.compareTo(obj2);
  });
  String pairsString = allKeys.join("");
  String sign = key + pairsString + key;
  String signString = generateMd5(sign).toUpperCase();
  parameter['sign'] = signString;
  return FormData.fromMap(new Map<String, dynamic>.from(parameter));
}
FormData getSign2(Map parameter) {
  var key = '537f9844eb30bd11e49e0fe702dd1e68';
  var timestamp = new DateTime.now().millisecondsSinceEpoch;
  parameter['app_key'] = "appm_api_h592655472";
  parameter['platformId'] = "92655472";
  parameter['timestamp'] = "$timestamp";
  List<String> allKeys = [];
  parameter.forEach((key, value) {
    if (!(value is MultipartFile)) {
      allKeys.add(key.toString() + value.toString());
    }
  });
  allKeys.sort((obj1, obj2) {
    return obj1.compareTo(obj2);
  });
  String pairsString = allKeys.join("");
  String sign = key + pairsString + key;
    print(sign);

  String signString = generateMd5(sign).toLowerCase();
  parameter['sign'] = signString;
  print(parameter);
  return FormData.fromMap(new Map<String, dynamic>.from(parameter));
}
String generateMd5(String data) {
  var content = new Utf8Encoder().convert(data);
  var digest = md5.convert(content);
  return hex.encode(digest.bytes);
}
